package org.wgc.study.job;

import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.item.ItemProcessor;
import org.springframework.batch.item.database.JdbcBatchItemWriter;
import org.springframework.batch.item.database.builder.JdbcBatchItemWriterBuilder;
import org.springframework.batch.item.file.FlatFileItemReader;
import org.springframework.batch.item.file.builder.FlatFileItemReaderBuilder;
import org.springframework.batch.item.file.mapping.BeanWrapperFieldSetMapper;
import org.springframework.batch.item.file.mapping.DefaultLineMapper;
import org.springframework.batch.item.file.transform.DelimitedLineTokenizer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.core.io.ClassPathResource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Component;
import org.wgc.study.vo.User;

import javax.sql.DataSource;

/***
 *@title DCJoB
 *@description <TODO description class purpose>
 *@author wgc_j
 *@version 1.0.0
 *@create 2025-08-04 15:59
 **/
@Component
public class ImportUserJob {
    @Autowired
    private JobBuilderFactory jobBuilderFactory;
    @Autowired
    private StepBuilderFactory stepBuilderFactory;
    @Autowired
    private DataSource dataSource;
    @Autowired
    private  NamedParameterJdbcTemplate jdbcTemplate;
    // 定义Job

    public FlatFileItemReader<User> itemReader() {
        // FlatFileItemReader 是一个加载普通文件的 ItemReader
        FlatFileItemReader<User> reader = new FlatFileItemReader<>();
        // 由于data.csv文件第一行是标题，因此通过setLinesToSkip方法设置跳过一行
        reader.setLinesToSkip(1);
        // setResource方法配置data.csv文件的位置
        reader.setResource(new ClassPathResource("users.csv"));
        // 通过setLineMapper方法设置每一行的数据信息
        reader.setLineMapper(new DefaultLineMapper<User>() {{
            setLineTokenizer(new DelimitedLineTokenizer() {{
                // setNames方法配置了data.csv文件一共有4列，分别是id、username、以及sex,
                setNames("name", "age", "email");
                // 配置列与列之间的间隔符（这里是空格）
                setDelimiter(",");
            }});
            setFieldSetMapper(new BeanWrapperFieldSetMapper() {{
                setTargetType(User.class);
            }});
        }});
        return reader;
    }

    public UserPreStatementSetter preStatementSetter(){
        return new UserPreStatementSetter();
    }

    public JdbcBatchItemWriter<User>  itemWriter(){
        return new JdbcBatchItemWriterBuilder<User>()
                .dataSource(dataSource)
                .sql("insert into users(name, age, email) values(?,?,?)")
                .itemPreparedStatementSetter(preStatementSetter())
                .build();
    }

    public Step step(){
        return stepBuilderFactory.get("step1")
                .<User, User>chunk(1)
                .reader(itemReader())
                .writer(itemWriter())
                .build();
    }

    @Bean
    public Job job(){
        return jobBuilderFactory.get("jdbc-writer-job")
                .start(step())
                .build();
    }

}
